package com.log.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.log.mapper.LogInfoMapper;
import com.log.model.dto.LogInfoDTO;
import com.log.model.entity.LogInfo;
import com.log.service.LogInfoService;
import com.log.util.JsonData;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassDescription:
 * @Author: zhaoyi
 * @Created: 2024/7/15 9:30
 */
@Service
@RequiredArgsConstructor
public class LogInfoServiceImpl extends ServiceImpl<LogInfoMapper, LogInfo> implements LogInfoService {

    private final LogInfoMapper mapper;

    /**
     * 3.1  服务B提供接口接收服务A发送的日志分析结果请求，并将数据内容（对比结果，日志时间）存到数据库;
     * @param dtoList
     * @return
     */
    @Override
    public JsonData syncAndInsertLog(List<LogInfoDTO> dtoList) {

        List<LogInfo> logInfoList = dtoListToEntityList(dtoList);
        if (CollectionUtils.isEmpty(logInfoList)) {
            return JsonData.buildSuccess();
        }
        this.saveBatch(logInfoList);
        return JsonData.buildSuccess();
    }

    @Override
    public JsonData addLog(LogInfoDTO logInfoDTO) {
        LogInfo logInfo = new LogInfo();
        BeanUtils.copyProperties(logInfoDTO,logInfo);
        logInfo.setCreatedTime(new Date());
        logInfo.setUpdateTime(new Date());
        this.save(logInfo);
        return JsonData.buildSuccess();
    }

    private List<LogInfo> dtoListToEntityList(List<LogInfoDTO> dtoList) {
        List<LogInfo> logInfoList = new ArrayList<>();
        for (LogInfoDTO logInfoDTO : dtoList) {
            LogInfo logInfo = new LogInfo();
            BeanUtils.copyProperties(logInfoDTO,logInfo);
            logInfo.setCreatedTime(new Date());
            logInfo.setUpdateTime(new Date());
            logInfoList.add(logInfo);
        }
        return logInfoList;
    }

    /**
     * 3.2 提供接口对存储的数据进行分页查询 条件查询;
     * @param logInfoDTO
     * @return
     */
    @Override
    public Map<String, Object> getPage(LogInfoDTO logInfoDTO) {

        Page<LogInfo> logInfoPage = mapper.selectPage(new Page<>(logInfoDTO.getPage(), logInfoDTO.getSize()), new LambdaQueryWrapper<LogInfo>()
                .eq(!StringUtils.isEmpty(logInfoDTO.getTag()), LogInfo::getTag, logInfoDTO.getTag())
                .like(!StringUtils.isEmpty(logInfoDTO.getLogText()), LogInfo::getLogText, logInfoDTO.getLogText())
                .like(!StringUtils.isEmpty(logInfoDTO.getDate()),LogInfo::getCreatedTime,logInfoDTO.getDate())
                .orderByAsc(LogInfo::getId));
        Map<String, Object> pageMap = new HashMap<>(3);

        pageMap.put("total_record", logInfoPage.getTotal());
        pageMap.put("total_page", logInfoPage.getPages());
        pageMap.put("current_data", logInfoPage.getRecords().stream().map(obj -> beanProcess(obj)).collect(Collectors.toList()));

        return pageMap;
    }

    private LogInfoDTO beanProcess(LogInfo logInfo) {
        LogInfoDTO dto = new LogInfoDTO();
        BeanUtils.copyProperties(logInfo, dto);
        return dto;
    }

    /**
     CompletableFuture.runAsync(() -> {

     });
     //等同于
     new Thread(() -> {

     }).start();
     */

        /*CompletableFuture<String> completableFuture = CompletableFuture.supplyAsync(() -> {
            return "";
        });

        //加入主线程
        String s = completableFuture.get();//拿到返回值
        completableFuture.join();*/
}
